package com.fish.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fish.common.result.Result;
import com.fish.model.dto.NotificationDTO;
import com.fish.model.dto.NotificationQueryDTO;
import com.fish.model.vo.NotificationDetailVO;
import com.fish.model.vo.NotificationVO;
import com.fish.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 通知Controller
 */
@Tag(name = "通知管理", description = "通知相关接口，包括发送通知、查询通知、标记已读等")
@Slf4j
@RestController
@RequestMapping("/api/notification")
public class NotificationController {
    
    @Autowired
    private NotificationService notificationService;
    
    /**
     * 发送通知
     */
    @Operation(summary = "发送通知", description = "发送系统通知给指定用户或角色")
    @PostMapping("/send")
    public Result<Long> sendNotification(@RequestBody NotificationDTO dto) {
        log.info("发送通知，标题：{}", dto.getTitle());
        Long notificationId = notificationService.sendNotification(dto);
        return Result.success("发送成功", notificationId);
    }
    
    /**
     * 分页查询我的通知
     */
    @Operation(summary = "查询我的通知", description = "分页查询当前用户的通知列表")
    @PostMapping("/my/list")
    public Result<IPage<NotificationVO>> getMyNotifications(@RequestBody NotificationQueryDTO dto) {
        log.info("查询我的通知");
        IPage<NotificationVO> page = notificationService.getMyNotifications(dto);
        return Result.success(page);
    }
    
    /**
     * 获取通知详情
     */
    @Operation(summary = "查询通知详情", description = "根据通知ID查询通知详细信息")
    @Parameter(name = "id", description = "通知ID", required = true, example = "1")
    @GetMapping("/{id}")
    public Result<NotificationDetailVO> getNotificationDetail(@PathVariable Long id) {
        log.info("查询通知详情，ID：{}", id);
        NotificationDetailVO vo = notificationService.getNotificationDetail(id);
        return Result.success(vo);
    }
    
    /**
     * 标记通知为已读
     */
    @Operation(summary = "标记通知已读", description = "标记指定通知为已读")
    @Parameter(name = "receiverId", description = "接收记录ID", required = true, example = "1")
    @PutMapping("/read/{receiverId}")
    public Result<String> markAsRead(@PathVariable Long receiverId) {
        log.info("标记通知为已读，receiverId：{}", receiverId);
        notificationService.markAsRead(receiverId);
        return Result.success("标记成功");
    }
    
    /**
     * 标记所有通知为已读
     */
    @Operation(summary = "标记所有通知已读", description = "将当前用户的所有未读通知标记为已读")
    @PutMapping("/read/all")
    public Result<String> markAllAsRead() {
        log.info("标记所有通知为已读");
        notificationService.markAllAsRead();
        return Result.success("标记成功");
    }
    
    /**
     * 删除通知（接收人删除）
     */
    @Operation(summary = "删除通知", description = "删除指定通知")
    @Parameter(name = "receiverId", description = "接收记录ID", required = true, example = "1")
    @DeleteMapping("/{receiverId}")
    public Result<String> deleteNotification(@PathVariable Long receiverId) {
        log.info("删除通知，receiverId：{}", receiverId);
        notificationService.deleteNotification(receiverId);
        return Result.success("删除成功");
    }
    
    /**
     * 获取未读通知数量
     */
    @Operation(summary = "查询未读通知数量", description = "查询当前用户的未读通知数量")
    @GetMapping("/unread/count")
    public Result<Long> getUnreadCount() {
        log.info("查询未读通知数量");
        Long count = notificationService.getUnreadCount();
        return Result.success(count);
    }
    
    /**
     * 撤回通知（发送者撤回）
     */
    @Operation(summary = "撤回通知", description = "撤回已发送的通知（仅发送者可操作）")
    @Parameter(name = "notificationId", description = "通知ID", required = true, example = "1")
    @PutMapping("/recall/{notificationId}")
    public Result<String> recallNotification(@PathVariable Long notificationId) {
        log.info("撤回通知，notificationId：{}", notificationId);
        notificationService.recallNotification(notificationId);
        return Result.success("撤回成功");
    }
}
